package admin

import (
	"github.com/gin-gonic/gin"
	"strconv"
)

type Resp struct {
	Code int64       `json:"code"`
	Msg  string      `json:"msg"`
	Data interface{} `json:"data"`
}

func Response(code int64, msg string, data interface{}) Resp {
	return Resp{code, msg, data}
}
func RespSucc(c *gin.Context, data interface{}) {
	c.JSON(200, Response(1000, "成功", data))
}

func RespList(c *gin.Context, total int64, data interface{}) {
	msp := map[string]interface{}{
		"total": total,
		"list":  data,
	}
	c.JSON(200, Response(1000, "成功", msp))
}

func RespErr(c *gin.Context, data error) {
	c.JSON(200, Response(4000, data.Error(), nil))
}
func RespData(c *gin.Context, code int64, msg string, data error) {
	c.JSON(200, Response(code, msg, data))
}

//获取分页
func pages(c *gin.Context) (count int64, size, limit int) {
	p := c.DefaultQuery("page", "1")
	s := c.DefaultQuery("size", "10")

	page, err := strconv.Atoi(p)
	if err != nil {
		page = 1
	}
	size, err = strconv.Atoi(s)
	if err != nil {
		size = 10
	}
	limit = (page - 1) * size
	return
}
